package 算法.leetcode.bytedance.字符串;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author lchenglong
 * @date 2023/2/16
 */
public class 人民币转大写 {

    public static class Solution {

        public static void main(String[] args) {
            int num = 1234587 ;
            String ans = Solution.NumberToChinese(num);
            System.out.println(ans);
        }
        public static String NumberToChinese(int num) {
            String result = "";
            if (num == 0) {
                return "零";
            }
            int _num = num;
            String[] chn_str = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
            String[] unit_value = new String[]{"", "拾", "佰", "仟"};
//            String[] chn_str = new String[] { "零","一", "二", "三", "四", "五", "六", "七", "八", "九" };
            String[] section_value = new String[]{"", "万", "亿", "万亿"};
//            String[] unit_value = new String[] { "", "十", "百", "千" };
            int section = _num % 10000;
            for (int i = 0; _num != 0 && i < 4; i++) {
                if (section == 0) {
                    //0不需要考虑节权值，不能出现连续的“零”
                    if (result.length() > 0 && result.substring(0, 1) != "零") {
                        result = "零" + result;
                    }
                    _num = _num / 10000;
                    section = _num % 10000;
                    continue;
                }
                result = section_value[i] + result;
                int unit = section % 10;
                for (int j = 0; j < 4; j++) {
                    if (unit == 0) {
                        //0不需要考虑位权值，不能出现联系的“零”，每节最后的0不需要
                        if (result.length() > 0 && result.substring(0, 1) != "零" && result.substring(0, 1) != section_value[i]) {
                            result = "零" + result;
                        }
                    } else {
                        result = chn_str[unit] + unit_value[j] + result;
                    }
                    section = section / 10;
                    unit = section % 10;
                }
                _num = _num / 10000;
                section = _num % 10000;
            }
            if (result.length() > 0 && result.substring(0, 1) == "零") {
                //清理最前面的"零"
                result = result.substring(1);
            }
            return result;
        }

    }


    public static class Main{
        public static final char[] chNumSet = "零壹贰叁肆伍陆柒捌玖".toCharArray();
        public static final char[] chPowSet = "分角元拾佰仟万拾佰仟亿拾佰仟万".toCharArray();

        public static void main(String[] args) {
            new Main().go();
        }

        private void go() {
            // 读入一个数值
            Scanner console = new Scanner(System.in);
            while (console.hasNext()) {
                double d = console.nextDouble();
                // 解析该数值为汉字形式
                long l = (long) (d * 100);
                String strRes = parse(l);
                System.out.println("人民币:" + strRes);
            }
        }

        private String parse(long n) {
            if (n == 0) {
                return "零元整";
            }

            StringBuilder sb = new StringBuilder();
            // 将数值解析为单个数值的数组
            int[] nums = toNum(n);
            for (int i = nums.length - 1; i >= 0; i--) {
                sb.append(chNumSet[nums[i]]);
                sb.append(chPowSet[i]);
            }

            String res = sb.toString().replaceAll("零[分角拾佰仟]", "零")
                    .replaceAll("零+", "零").replaceAll("零+元", "元")
                    .replaceAll("零+万", "万").replaceAll("零+亿", "亿")
                    .replaceAll("亿万", "亿");
            if (res.startsWith("壹拾")) {
                res = res.substring(1);
            }

            if (res.endsWith("零")) {
                res = res.substring(0, res.length() - 1);
            }

            if (res.endsWith("元")) {
                res = res + "整";
            }
            return res;
        }

        private int[] toNum(long n) {
            int[] res = new int[0];
            while (n != 0) {
                int end = (int) (n % 10L);
                // 扩展数组的长度
                res = Arrays.copyOf(res, res.length + 1);
                res[res.length - 1] = end;
                n /= 10L;
            }
            return res;
        }
    }
}
